#include <math.h>
#include <stdio.h>

struct rational {
	int x, y;
};

int part1(struct rational z)
{
	return z.x;
}

int part2(struct rational z)
{
	return z.y;
}


int greatestCommonDivisor(int a,int b)
{
	if (a%b == 0)
		return b;
	else {
		int recurse = greatestCommonDivisor(b,a%b);
		return recurse;
	}
}

struct rational reduce(struct rational z) {
    if (z.y == 0) {
        printf("error：分母不能为0！\n");
        exit(1);  
    }
    if (z.y < 0) {
        z.x = -z.x;
        z.y = -z.y;
    }
    int common = greatestCommonDivisor(z.x, z.y);
    z.x /= common;
    z.y /= common;
    return z;
}

struct rational make_rational(int x, int y)
{
	struct rational z;
	z.x = x;
	z.y = y;
	return reduce(z);
}


struct rational add_rational(struct rational a1, struct rational a2)
{
	return make_rational(part1(a1) * part2(a2) + part1(a2) * part2(a1),
				  part2(a1) * part2(a2));
}

struct rational sub_rational(struct rational a1, struct rational a2)
{
	return make_rational(part1(a1) * part2(a2) - part1(a2) * part2(a1),
				  part2(a1) * part2(a2));
}

struct rational mul_rational(struct rational a1, struct rational a2)
{
	return make_rational(part1(a1) * part1(a2),
				 part2(a1) * part2(a2));
}

struct rational div_rational(struct rational a1, struct rational a2)
{
	return make_rational(part1(a1) * part2(a2),
				 part2(a1) * part1(a2));
}

void print_rational(struct rational z) {
    if (z.y == 1) {
        printf("%d\n", z.x);
    } else {
        printf("%d/%d\n", z.x, z.y);
    }
}

int main(void)
{
	struct rational a = make_rational(1, 8); /* a=1/8 */
	struct rational b = make_rational(-1, 8); /* b=-1/8 */
	print_rational(add_rational(a, b));
	print_rational(sub_rational(a, b));
	print_rational(mul_rational(a, b));
	print_rational(div_rational(a, b));

	return 0;
}